<?php

namespace App\Http\Controllers\WxApi\Handles;

use App\Http\Controllers\WxApi\Handle;
use App\Http\Controllers\WxApi\Traits\Reply;
use Houdunren\WeChat\Button;
use Illuminate\Support\Facades\Log;

class Event extends Handle
{
    use Reply;

    public function handle()
    {

        foreach (['subscribe', 'button'] as $method) {
            if ($content = $this->$method()) {
                return $content;
            }
        }
    }

    protected function button()
    {

        $button = app(Button::class);
        if ($button->isClick()) {
            return $this->reply($this->message->EventKey);
        }
    }

    protected function subscribe()
    {

        if ($this->message->isSubscribe()) {
            //支持微信关注，先查看是否匹配关键词
            $welcome = $this->model->welcome ?? '感谢您的关注';

            if ($content = $this->reply($welcome)) {
                return $content;
            }

            return $this->message->text($welcome);
        }
    }
}
